﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YJKPresentation.CommonUnit;
using YJKPresentation.Utils;

namespace YJKPresentation.SingleFloor
{
	public partial class TransformTypeForm : System.Windows.Forms.Form
	{
		public bool DialogResultIsSelecteElement
		{
			get
			{
				return this.m_DialogResultIsSelecteElement;
			}
		}

		public TransformTypeForm(ExternalCommandData cmdData, SelectionSetManager selectionSetManager)
		{
			this.m_Revit = cmdData;
			this.m_SelectionSetManager = selectionSetManager;
			this.InitializeComponent();
			this.InitializeData();
			this.m_TextEditBox.KeyDown += this.TextEditBox_KeyDown;
			this.m_TextEditBox.ImeMode = ImeMode.OnHalf;
			this.m_TextEditBox.Visible = false;
		}

		protected void InitializeData()
		{
			foreach (SelectionSet selectionSet in this.m_SelectionSetManager.SelectionSetList)
			{
				this.LBSelectionSet.Items.Add(selectionSet.SetName);
			}
			if (this.LBSelectionSet.Items.Count > 0)
			{
				this.LBSelectionSet.SelectedIndex = this.LBSelectionSet.Items.Count - 1;
			}
			this.UpdateTypeStateList();
			this.cmbMappingType.SelectedIndex = 0;
			this.tbTolerance.Text = "300";
			this.Highlight();
		}

		private void UpdateTypeStateList()
		{
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return;
			}
			this.chblFilter.Focus();
			SelectionSet selectionSet = this.m_SelectionSetManager.SelectionSetList[this.LBSelectionSet.SelectedIndex];
			this.chblFilter.Items.Clear();
			for (int i = 0; i < selectionSet.TypeSelectedStateList.Count; i++)
			{
				SelectionSet.TypeSelectedState typeSelectedState = selectionSet.TypeSelectedStateList[i];
				string itemName = typeSelectedState.TheType.m_Category;
				if (selectionSet.TypeSelectedStateList.FindAll((SelectionSet.TypeSelectedState s) => s.TheType.m_Category.CompareTo(itemName) == 0).Count > 1)
				{
					itemName = itemName + "(" + typeSelectedState.TheType.m_Type + ")";
				}
				this.chblFilter.Items.Add(itemName);
				this.chblFilter.SetItemChecked(i, typeSelectedState.TheChecked);
			}
		}

		public List<Element> SelectElements()
		{
			List<Element> list = new List<Element>();
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return list;
			}
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			foreach (ElementId elementId in this.m_SelectionSetManager.SelectionSetList[this.LBSelectionSet.SelectedIndex].SelectedElementIdList)
			{
				Element element = null;
				try
				{
					element = document.GetElement(elementId);
				}
				catch (Exception)
				{
					continue;
				}
				if (element != null)
				{
					list.Add(element);
				}
			}
			return list;
		}

		public List<ComponentType> SelectedTypeList()
		{
			List<ComponentType> list = new List<ComponentType>();
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return list;
			}
			foreach (SelectionSet.TypeSelectedState typeSelectedState in this.m_SelectionSetManager.SelectionSetList[this.LBSelectionSet.SelectedIndex].TypeSelectedStateList)
			{
				if (typeSelectedState.TheChecked)
				{
					list.Add(typeSelectedState.TheType);
				}
			}
			return list;
		}

		public double Tolerance()
		{
			return Common.MMToFeet(Convert.ToDouble(this.tbTolerance.Text));
		}

		public bool IsByWall()
		{
			return this.cmbMappingType.SelectedIndex == 0;
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return;
			}
			SelectionSet selectionSet = this.m_SelectionSetManager.SelectionSetList[this.LBSelectionSet.SelectedIndex];
			List<SelectionSet.TypeSelectedState> typeSelectedStateList = selectionSet.TypeSelectedStateList;
			for (int i = 0; i < this.chblFilter.Items.Count; i++)
			{
				this.chblFilter.SetItemChecked(i, true);
				typeSelectedStateList[i].TheChecked = true;
			}
			selectionSet.TypeSelectedStateList = typeSelectedStateList;
		}

		private void btnSelNone_Click(object sender, EventArgs e)
		{
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return;
			}
			SelectionSet selectionSet = this.m_SelectionSetManager.SelectionSetList[this.LBSelectionSet.SelectedIndex];
			List<SelectionSet.TypeSelectedState> typeSelectedStateList = selectionSet.TypeSelectedStateList;
			for (int i = 0; i < this.chblFilter.Items.Count; i++)
			{
				this.chblFilter.SetItemChecked(i, false);
				typeSelectedStateList[i].TheChecked = false;
			}
			selectionSet.TypeSelectedStateList = typeSelectedStateList;
		}

		private void btnSelContrary_Click(object sender, EventArgs e)
		{
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return;
			}
			SelectionSet selectionSet = this.m_SelectionSetManager.SelectionSetList[this.LBSelectionSet.SelectedIndex];
			List<SelectionSet.TypeSelectedState> typeSelectedStateList = selectionSet.TypeSelectedStateList;
			for (int i = 0; i < this.chblFilter.Items.Count; i++)
			{
				bool flag = !this.chblFilter.GetItemChecked(i);
				this.chblFilter.SetItemChecked(i, flag);
				typeSelectedStateList[i].TheChecked = flag;
			}
			selectionSet.TypeSelectedStateList = typeSelectedStateList;
		}

		private void tbTolerance_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int num = int.Parse(this.tbTolerance.Text);
				if (num < 0 || num > 1000)
				{
					YJKMessageBox.Show("请输入一个0到1000的整数.");
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				YJKMessageBox.Show("请输入一个0到1000的整数.");
				e.Cancel = true;
			}
		}

		private void TransformTypeForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
		}

		private void TransformTypeForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK)
			{
				if (!this.m_DialogResultIsSelecteElement)
				{
					if (!this.m_DialogResultIsSelecteElement && this.m_SelectionSetManager.SelectionSetList.Count < 1)
					{
						YJKMessageBox.Show("当前选择集为空，请先选择实体");
						e.Cancel = true;
						return;
					}
					int i = 0;
					int num = 0;
					while (i < this.chblFilter.Items.Count)
					{
						if (this.chblFilter.GetItemChecked(i))
						{
							num++;
						}
						i++;
					}
					if (num == 0)
					{
						YJKMessageBox.Show("当前选择集为空，过滤条件过滤掉了所有类型");
						e.Cancel = true;
						return;
					}
				}
				this.SaveCompornentData();
				for (int j = 0; j < this.LBSelectionSet.Items.Count; j++)
				{
					this.m_SelectionSetManager.SelectionSetList[j].SetName = this.LBSelectionSet.Items[j].ToString();
				}
			}
			RevitVersionFuncs.ClearSelection(this.m_Revit.Application.ActiveUIDocument.Selection);
		}

		private void LoadCompornentData()
		{
			string path = Path.GetTempPath() + "TransformTypeForm.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.cmbMappingType.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			this.tbTolerance.Text = streamReader.ReadLine();
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "TransformTypeForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.cmbMappingType.SelectedIndex);
			streamWriter.WriteLine(this.tbTolerance.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void Highlight()
		{
			Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
			this.SelectedTypeList();
			List<Element> selElements = this.SelectElements();
			RevitVersionFuncs.SetSelection(selection, selElements);
		}

		private bool IsValidType(Element element, List<ComponentType> selectTypeList)
		{
			string name = element.GetType().Name;
			string name2 = element.Category.Name;
			ComponentType componentType = new ComponentType(name, name2);
			return selectTypeList.FindIndex((ComponentType t) => t.IsEquals(componentType)) >= 0;
		}

		private void chblFilter_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return;
			}
			SelectionSet selectionSet = this.m_SelectionSetManager.SelectionSetList[this.LBSelectionSet.SelectedIndex];
			List<SelectionSet.TypeSelectedState> typeSelectedStateList = selectionSet.TypeSelectedStateList;
			for (int i = 0; i < this.chblFilter.Items.Count; i++)
			{
				bool itemChecked = this.chblFilter.GetItemChecked(i);
				typeSelectedStateList[i].TheChecked = itemChecked;
			}
			selectionSet.TypeSelectedStateList = typeSelectedStateList;
			this.Highlight();
		}

		private void LBSelectionSet_DrawItem(object sender, DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.DrawFocusRectangle();
			if (e.Index < 0)
			{
				return;
			}
			e.Graphics.DrawString(this.LBSelectionSet.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
		}

		private void TextEditBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
			{
				if (this.m_TextEditBox.Text.Trim().CompareTo("") != 0 && this.LBSelectionSet.SelectedIndex >= 0)
				{
					this.LBSelectionSet.Items[this.LBSelectionSet.SelectedIndex] = this.m_TextEditBox.Text;
				}
				this.m_TextEditBox.Visible = false;
			}
			if (e.KeyCode == Keys.Escape)
			{
				this.m_TextEditBox.Visible = false;
			}
		}

		private void UpdateSelectionSetNameToTextEditBox()
		{
			if (this.m_TextEditBox.Visible)
			{
				if (this.m_EditIndex != -1 && this.m_TextEditBox.Text.Trim().CompareTo("") != 0)
				{
					this.LBSelectionSet.Items[this.m_EditIndex] = this.m_TextEditBox.Text;
				}
				this.m_TextEditBox.Visible = false;
			}
		}

		private void LBSelectionSet_MouseClick(object sender, MouseEventArgs e)
		{
			this.UpdateSelectionSetNameToTextEditBox();
			if (this.LBSelectionSet.SelectedIndex >= 0)
			{
				this.UpdateTypeStateList();
				this.Highlight();
			}
			this.m_EditIndex = -1;
		}

		private void LBSelectionSet_DoubleClick(object sender, EventArgs e)
		{
			if (this.LBSelectionSet.SelectedIndex < 0)
			{
				return;
			}
			int selectedIndex = this.LBSelectionSet.SelectedIndex;
			string text = this.LBSelectionSet.Items[selectedIndex].ToString();
			System.Drawing.Rectangle itemRectangle = this.LBSelectionSet.GetItemRectangle(selectedIndex);
			this.m_TextEditBox.Parent = this.LBSelectionSet;
			this.m_TextEditBox.Bounds = itemRectangle;
			this.m_TextEditBox.Multiline = true;
			this.m_TextEditBox.Visible = true;
			this.m_TextEditBox.Text = text;
			this.m_TextEditBox.Focus();
			this.m_TextEditBox.SelectAll();
			this.m_EditIndex = selectedIndex;
			this.UpdateTypeStateList();
			this.Highlight();
		}

		private void btnAddSet_Click(object sender, EventArgs e)
		{
			this.UpdateSelectionSetNameToTextEditBox();
			this.m_DialogResultIsSelecteElement = true;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.UpdateSelectionSetNameToTextEditBox();
			this.m_DialogResultIsSelecteElement = false;
		}

		private ExternalCommandData m_Revit;

		private SelectionSetManager m_SelectionSetManager;

		private System.Windows.Forms.TextBox m_TextEditBox = new System.Windows.Forms.TextBox();

		private int m_EditIndex = -1;

		private bool m_DialogResultIsSelecteElement;
	}
}
